<template>
    <div class="keppel">
        <div class="return">
            <img src="http://assets.rurushishi.com/activies/tjkeppel/return.png" alt="" @click="returns">
        </div>
        <music></music>
        <share :isshare="isshare"></share>
        <!-- <Bottom :baseText="baseText"></Bottom> -->
    </div>
</template>
<script>
import Cookies from "js-cookie";
import { MessageBox } from "mint-ui";
import request from "@/utils/request";
import * as config from "./config";
import music from "./components/music";
import Share from "@/components/common/Share";
// import Bottom from "@/components/common/Bottom";
export default {
    data(){
        return{

            openid: Cookies.get('openid'),
            saveInfo:{
                channel:'',
                nickname: '',
                openid: Cookies.get('openid'),
                reOpenid: '',
                headImg: '',
                six: '',
            },
            userInfo: null,
            // 接收到的传播信息
            receiveSpreadInfo: {
                channel: 0, //渠道
                fuid: "", //传播者的openid
                ouid: "" //销售或渠道的openid
            },
            isshare: false,//是否分享
            // baseText: config.BASE_TEXT,
            isplay: false,
            isJoinfos: false
        }
    },
    components:{
        share: Share,
        // Bottom: Bottom,
        music: music,
    },
    methods:{
         // 是否参加过活动
        isJoinfo(){
            let _self =this;
            request
                .get(`${_self.global.API_TJKEPPEL_ACT_URL}activity/logic/userJoinfo`,
                {
                //    openid: _self.saveInfo.openid,
                }).then(function(data){
                    if(!data.data){
                        //没有参加过活动
                         _self.isJoinfos = false;
                         _self.init();
                    }else{
                       _self.isJoinfos = true; //参加过活动
                       _self.init();
                    } 
                });
        },
        returns(){
            this.pushUrl('index');
        },
         pushUrl(page) {
            // 带上本页面的传播信息
            let url = "";
            let fullPathNoShare = this.$route.fullPath;
            fullPathNoShare = fullPathNoShare.replace("?isshare=1", "");
            fullPathNoShare = fullPathNoShare.replace("&isshare=1", "");
            fullPathNoShare = fullPathNoShare.replace("?personal=2", "");
            fullPathNoShare = fullPathNoShare.replace("&personal=2", "");
            if (fullPathNoShare.indexOf("?") > -1) {
                url = page + "?" + fullPathNoShare.split("?")[1];
            } else {
                url = page;
            }
            this.$router.push("/" + url);
        },
         shareCallBack(){
            let _self = this;
            let query={
                        channel: _self.saveInfo.channel,
                        openid : _self.saveInfo.openid,
                        prizeCode: 'prize_1',
                        referenceOpenid: _self.saveInfo.reOpenid
                    };
            let obj = JSON.stringify(query); 
            request
                .post(
                    `${_self.global.API_TJKEPPEL_ACT_URL}activity/logic/share`,
                    obj,{needDealEx:false}
                )
                .then(function(data) {
                    if(data.code==0){
                        this.isshare=false;
                    }
            });
       },
       init() {
            let that = this;
            // 设置传播信息
            if (this.$route.query.channel)
                this.receiveSpreadInfo.channel = this.$route.query.channel;
                this.saveInfo.channel = this.$route.query.channel;
            if (this.$route.query.fuid)
                this.receiveSpreadInfo.fuid = this.$route.query.fuid;
                this.saveInfo.reOpenid = this.$route.query.fuid;
            if (this.$route.query.ouid)
                this.receiveSpreadInfo.ouid = this.$route.query.ouid;
            if (this.$route.query.isshare) that.isshare = true;
            config.getUserInfo().then(function(userInfo) {
                console.log(userInfo)
                that.userInfo = userInfo;
                // 设置分享 标题 链接 回调
                let shareObj = {
                    title: "",
                    link: ""
                };
                if (
                that.userInfo &&
                that.userInfo.user &&
                that.userInfo.user.nickname
                ) {
                that.saveInfo.nickname = that.userInfo.user.nickname;
                that.saveInfo.six = that.userInfo.user.six;
                that.saveInfo.headImg = that.userInfo.user.headimgurl;
                shareObj.title = `${
                    that.userInfo.user.nickname
                }520告白日｜怦然心动，你的“美”一刻”`;
                }

                if (that.userInfo.salerOrAgent&&that.isJoinfos) {
                // 是销售或者渠道,更新ouid
                shareObj.link = `${config.SHARE_LINK}?channel=${
                    that.receiveSpreadInfo.channel
                }&fuid=${that.openid}&ouid=${that.openid}`;
                } else if(!that.isJoinfos){ //没有参加活动直接分享的
                     shareObj.link = `${config.SHARE_NOJOIN_LINK}?channel=${
                    that.receiveSpreadInfo.channel
                }&fuid=${that.openid}${
                    that.receiveSpreadInfo.ouid
                    ? "&ouid=" + that.receiveSpreadInfo.ouid
                    : ""
                }`;   
                }else {
                shareObj.link = `${config.SHARE_LINK}?channel=${
                    that.receiveSpreadInfo.channel
                }&fuid=${that.openid}${
                    that.receiveSpreadInfo.ouid
                    ? "&ouid=" + that.receiveSpreadInfo.ouid
                    : ""
                }`;
                }
                config.initShare(shareObj);
                that.global.SHARE_CALLBACK_OK = function(type) {
                that.shareCallBack();
                };
            });   
        }
    },
    created(){
        this.isJoinfo();
    }
}
</script>
<style lang="stylus" scoped>
.keppel{
    position absolute
    top 0
    left 0
    right 0
    bottom 0
    width 100%
    height 18.27rem
    background url(http://assets.rurushishi.com/activies/tjkeppel/rule2.jpg) no-repeat
    background-size 100% 100%
}
.return{
    width 100%
    position absolute
    bottom 3.3rem
    text-align center
}
.return>img{
    width 3.14rem
    height .93rem
}
</style>

